home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / Apps / DevTools / eText5 / Source / NewXTeXT / XText0.9beta2 / XText.subproj / XTAction.m < prev    next >
Encoding:
Text File  |  1995-07-28  |  3.6 KB  |  217 lines

  1. #import "XTAction.h"
  2. #import "XText.h"
  3. #import "ErrorStream.h"
  4. #import <appkit/Application.h>
  5. #import <stdio.h>
  6. #import <string.h>
  7. #import <appkit/NXCType.h>
  8.  
  9.  
  10.  
  11. @implementation XTAction
  12.  
  13. static id undefined_action = 0;
  14.  
  15. + undefinedAction
  16. {
  17.     if (!undefined_action)
  18.         undefined_action = [[XTAction allocFromZone:[NXApp zone]] init];
  19.     return undefined_action;
  20. }
  21.  
  22. - applyTo:xtext event:(NXEvent *)event
  23. {
  24.     [xtext unboundKey];
  25.     return self;
  26. }
  27.  
  28. @end
  29.  
  30. @implementation XTMsg0Action
  31.  
  32. - initSel:(SEL)sel
  33. {
  34.     [super init];
  35.     action_sel = sel;
  36.     return self;
  37. }
  38.  
  39. - applyTo:xtext event:(NXEvent *)event
  40. {
  41.     return [xtext perform:action_sel];
  42. }
  43.  
  44. @end
  45.  
  46. @implementation XTMsg1Action
  47.  
  48. - initSel:(SEL)sel arg:(int)arg
  49. {
  50.     [super init];
  51.     action_sel = sel;
  52.     action_arg = arg;
  53.     return self;
  54. }
  55.  
  56. - applyTo: xtext event:(NXEvent *)event
  57. {
  58.     return [xtext perform:action_sel with:(id)action_arg];
  59. }
  60.  
  61. @end
  62.  
  63. @implementation XTMsg2Action
  64.  
  65. - initSel:(SEL)sel arg:(int)arg1 arg:(int)arg2
  66. {
  67.     [super init];
  68.     action_sel = sel;
  69.     action_arg1 = arg1;
  70.     action_arg2 = arg2;
  71.     return self;
  72. }
  73.  
  74. - applyTo: xtext event:(NXEvent *)event
  75. {
  76.     return [xtext perform:action_sel
  77.                     with:(id)action_arg1 with:(id)action_arg2];
  78. }
  79.  
  80. @end
  81.  
  82. @implementation XTDispatchAction
  83.  
  84. - init
  85. {
  86.     charCode k;
  87.  
  88.     [super init];
  89.  
  90.     for (k=0; k<CHAR_CODES; ++k) actions[k] = nil;
  91.     
  92.     return self;
  93. }
  94.  
  95. // provided for backward compatability
  96. - initBase:(const char *)base estream:errs
  97. {
  98.     charCode k;
  99.  
  100.     [super init];
  101.     for (k=0; k<CHAR_CODES; ++k) actions[k] = nil;
  102.     if (!strcmp(base,"none")) {}
  103.  
  104. //    else if (!strcmp(base,"other_base"))
  105. //        initbase_other_base(actions)
  106.  
  107.     else {}
  108.     return self;
  109. }
  110.  
  111.  
  112. // a convenience method to loading keybindings
  113. - loadFromFile:(char *)fullName estream:errs
  114. {
  115.     FILE *fp;
  116.     int i;
  117.     char line[256];
  118.  
  119.     fp = fopen(fullName, "r");
  120.     
  121.     if(!fp){
  122.         char msg[100];
  123.         sprintf(msg,"Cannot read %s", fullName);
  124.         [(errs ? errs : [ErrorStream default]) report:msg];
  125.         return self;
  126.     }
  127.     
  128.     while(fgets(line, 256, fp) != NULL){
  129.         if(line[0] == '#' ){} // a comment
  130.         else{
  131.             for(i=0;i<strlen(line);i++){
  132.                 if(line[i] == '\n' || line[i] == '\t') line[i] = ' ';
  133.             }
  134.             [self addBindings:line estream:errs];
  135.         }
  136.     }
  137.                 
  138.     return self;
  139. }
  140.  
  141. - bindKey:(charCode)key toAction:action estream:errs
  142. {
  143.     if ((key < 0) || (key >= CHAR_CODES)) {
  144.         char msg[40];
  145.         sprintf(msg, "Invalid key code: %d", key);
  146.         [(errs ? errs : [ErrorStream default]) report:msg];
  147.     } else
  148.         actions[key] = action;
  149.     return self;
  150. }
  151.  
  152. - applyTo:xtext event:(NXEvent *)event
  153. {
  154.     charCode k = event->data.key.charCode << NUM_MASKS;
  155.     id action;
  156.  
  157.     if ((k >= 0) && (k < CHAR_CODES)) {
  158.         if (event->flags & NX_ALPHASHIFTMASK)    k += 1;
  159.         if (event->flags & NX_SHIFTMASK)          k += 2;
  160.         if (event->flags & NX_CONTROLMASK)        k += 4;
  161.         if (event->flags & NX_ALTERNATEMASK)      k += 8;
  162.         if (event->flags & NX_COMMANDMASK)        k += 16;
  163.         if (event->flags & NX_NUMERICPADMASK)     k += 32;
  164.         if (event->flags & NX_HELPMASK)           k += 64;
  165.  
  166.         action = actions[k];
  167.         if (action) return [action applyTo:xtext event:event];
  168.         
  169.         /* also try action without the caps lock bit on */
  170.         if(event->flags & NX_ALPHASHIFTMASK){
  171.             k-=1;
  172.             action = actions[k];
  173.             if (action) return [action applyTo:xtext event:event];
  174.         }
  175.     }
  176.     return nil;
  177. }
  178.  
  179. @end
  180.  
  181. @implementation XTEventMsgAction
  182.  
  183. - initSel:(SEL)sel
  184. {
  185.     [super init];
  186.     action_sel = sel;
  187.     return self;
  188. }
  189.  
  190. - applyTo:xtext event:(NXEvent *)event
  191. {
  192.     return [xtext perform:action_sel with:(id)event];
  193. }
  194.  
  195. @end
  196.  
  197. @implementation XTSeqAction
  198.  
  199. - initLength:(int)len actions:(XTAction **)acts
  200. {
  201.     [super init];
  202.     length = len;
  203.     actions = acts;
  204.     return self;
  205. }
  206.  
  207. - applyTo:xtext event:(NXEvent *)event
  208. {
  209.     int i;
  210.  
  211.     for (i=0; i<length; ++i)
  212.         [actions[i] applyTo:xtext event:event];
  213.     return self;
  214. }
  215.  
  216. @end
  217.